 function basePath(){
    //获取当前网址，如： http://localhost:8080/ems/Pages/Basic/Person.jsp
    var curWwwPath = window.document.location.href;
    //获取主机地址之后的目录，如： /ems/Pages/Basic/Person.jsp
    var pathName = window.document.location.pathname;
    var pos = curWwwPath.indexOf(pathName);
    //获取主机地址，如： http://localhost:8080
    var localhostPath = curWwwPath.substring(0, pos);
    //获取带"/"的项目名，如：/ems
    var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
    //获取项目的basePath   http://localhost:8080/ems/
    var basePath=localhostPath+projectName+"/";
    return basePath;
};

jQuery(document).ready(function() {
	
	initTable();
	
	App.setPage("flowdeploy");  //Set current page
	App.init(); //Initialise plugins and elements
	

	//列表查询事件
	$('#search').click(function(){
		$('#table').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
	});
	
	
	//流程部署
	$('#flowDeploy').click(function(){
		$('#title').html("流程部署");
		$("#modalDeploy").modal('show');
	});
	
	$("#file").on("change",function(){
        var filePath=$(this).val();

      $("#uploadFileName").html("");

        if(filePath){
            var arr=filePath.split('\\');
            var fileName=arr[arr.length-1];
            $("#uploadFileName").html(fileName);
        }else{
            $("#uploadFileName").html("您未上传文件，或者您上传文件类型有误！").show();
        }
    });
	
	//上传流程图事件
	$('#uploadFlow').click(function(){
		var formData = new FormData($( "#uploadForm" )[0]);  

	     $.ajax({  
	          url: basePath() + 'deploy/upload.do' ,  
	          type: 'POST',  
	          data: formData,  
	          async: false,  
	          cache: false,  
	          contentType: false,  
	          processData: false,  
	  	      dataType: "json",
	          success: function (data) {
	        	  window.wxc.xcConfirm(data.info, window.wxc.xcConfirm.typeEnum.success,{
	  				onOk:function(v){
	  					if(data.result == 1){
	  						$('#table').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
	  			    	} 
	  				}
	  			});	
	      		$("#modalDeploy").modal('hide');
	          }
	     });  	
	});
	
	
	//清空表单数据事件
	$("#modalDeploy").on("hidden.bs.modal", function() {
		
      	$('#uploadFileName').html('');
      	$('[name="deployName"]').val('');
      	
//      	$('#file').;
      	$('#file').after($('#file').clone().val(''));
      	$('#file').remove();
      	
	});
});		



function initTable(){		
	$('#table').bootstrapTable({
		method: 'post',
	    url: basePath() + 'deploy/list.do',
	    pagination: true, //分页
	    dataType: "json",
        contentType: "application/json",
        striped: true,      //是否显示行间隔色
        sidePagination: "server",
	    columns: [
//	              {  
//            field : 'number',  
//            title : '序号',  
//            align : 'center',
//            width : '80px',
//            formatter : function(value, row, index) {  
//// 		                return index + 1;  
//                var page = $('#table').bootstrapTable("getPage");  
//                return page.pageSize * (page.pageNumber - 1) + index + 1;  
//            }  
//        }, 
        {
	        field: 'deployId',
	        title: '流程编号'
	    }, {
	        field: 'deployName',
	        title: '流程名称'
	    }, {
	        field: 'key',
	        title: 'KEY'
	    }, {
	        field: 'version',
	        title: '版本号'
	    },{
	        field: 'deployTime',
	        title: '部署时间'
	    },  {
            field: 'operate',
            title: '操作',
            align: 'center',
            width: '300px',
            events: operateEvents,
            formatter: operateFormatter
        } ]
	});
}

function operateFormatter(value, row, index) {
    return [            
            '<button type="button" class="Delete btn btn-danger  btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-minus"></span>删除</button>',
            '<button type="button" class="ShowImage btn btn-info  btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-eye-open"></span>流程图</button>'
    ].join('');
}

window.operateEvents = {
    'click .Delete': function (e, value, row, index) {

		var deploymentId = row.deployId;

		window.wxc.xcConfirm('是否<strong>删除</strong>流程部署数据?<br /><span style="color:red;">['+ deploymentId +']</span>', window.wxc.xcConfirm.typeEnum.confirm,{
			onOk:function(v){
				$.ajax({ 
					url: basePath() + 'deploy/delete.do',
					data: {"deploymentId": deploymentId},
					dataType: "json",
					success: function(data){	            						
						if(data.result == 1){
				        	$('#table').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
						}
			      	}
				});
			}
		});
    },
    'click .ShowImage': function (e, value, row, index) {
		var deploymentId = row.deployId;
    	
    	window.open("../../../deploy/showFlowImage.do?deploymentId=" + deploymentId,'top');

////		$("#modalShowImage").modal('show');
////		return;
//		
//		var deploymentId = row.deployId;
//		
//		$.ajax({ 
//			url: basePath() + 'deploy/showFlowImage.do',
//			data: {"deploymentId": deploymentId},
//			dataType: "json",
//			success: function(data){	
//				$.alert(JSON.stringify(data.datas.url));
//
//				
////				if(data.result == 1){
//////    	            $.alert('删除pbm['+ dictcode +']成功!');
//////		        	$('#table').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
////				}
//	      	}
//		});
    }
 };

function check(data){	
	var result = true;
	

	if($('#tableDictDetail').find('tbody').find('tr').not('.no-records-found').length == 0){
		window.wxc.xcConfirm('请增加<span style="color:red;"><strong>字典明细</strong></span>的行数据！', window.wxc.xcConfirm.typeEnum.info);
		return false;
	}
	
	
	var mustStr = "dictcode,dictname";
	
	var strMsg = "";
	var mustStrs = mustStr.split(',');
	for(var i=0; i<mustStrs.length; i++){
		var obj = $('[name="'+mustStrs[i]+'"]');
		if(obj.val() == ""){
			strMsg += obj.attr("placeholder") + "！<br />";
			
			result = false;
		}
	}
		
	
	if(strMsg != ""){
		window.wxc.xcConfirm(strMsg, window.wxc.xcConfirm.typeEnum.error);
	}
	
	return result;
}